These are quick step-by-step guides I wrote when going through Andy’s fundamentas of package development notes.
- Initialize package dev files with
devtools::create_package()
which creates mandatory file for the package - Enable git tracking:
- configure: usethis::use_git_configure()
- commit: usethis::use_git()
- Fill in sections of the DESCRIPTION file.
{roxygen2}
is used to actualize this. - Edit.Rprofile() to set options you’ll use in the documentation such as author details. This uses
usethis::edit_r_profile()
- Create a separate R file for each function you’ll be including in the package. Use
use_r("/path-to-file/R/function-name.R")
- Load the functions with
devtools::load_all()
- Check the loaded files with
check()
which runs R CMD checks to catch errors/warnings/notes that need addressing. - Add files you don’t wish to include in the package build-in
.Rbuildignore
file. - Enable roxygen2 to be used for package documentation: project options -> Build Tools -> check to generate documentation with roxygen or better
devtools::document()
which generates NAMESPACE automatically - Automate external function imports with
usethis::use_import_from()
: example usethis::use_import_from(“utils”, “install.packages”) - Document functions: put the cursor inside the R function definition and ctrl+shift+alt+R to insert the roxygen skeleton. The workflow here is after documenting -> load_all() -> document() -> check()
- Data files go to /data dir. It should be of .rda format
- External (non .rda format) data go to /inst/extdata/. Document them in the R file e.g. data.R and store it in /R.
load_all()
thendocument()
- Call
use_package_doc()
to add a dummy .R file that will prompt roxygen to generate basic package-level documentation. I noticed doing this erased imports in{package-name}-package.R
file. Add recommended imports (see 10) andcheck()
- Install your package with
devtools::install()
- Attach your package as with other packages by calling
library()
- Testing: Using the edit code -> load_all() -> experiment iteration can be unsustainable if you come back to your code months after development. You should write formal tests supported by
{testthat}
package. - Set up formal testing of your package with
usethis::use_testthat()
. Creates a folder /tests. Don’t edit tests/testhat.R - Call
usethis::use_test()
e.g.,use_test("install_load_packages.R")
to edit tests for functions living in a particular R file in R/. test()
or ctr + shift + T runs all tests in your test/ directory. The workflow updates to load_all() -> test() -> document() -> check(). Tests should be small and run quickly.- Dependencies, add imports in DESCRIPTION with
use_package()
. - Add README with
use_readme_rmd()
- Render readme.rmd with
build_readme()
- Use continuous integration with
use_github_action()
then build_readme() again 25 Build a website for your package withuse_pkgdown_github_pages()
then document().
Citation
BibTeX citation:
@online{okola2023,
author = {Okola, Basil},
title = {R Package Development Guide},
date = {2023-12-16},
url = {https://bokola.github.io/posts/2023-12-16-R-package dev/},
langid = {en}
}
For attribution, please cite this work as:
Okola, Basil. 2023. “R Package Development Guide.” December
16, 2023. https://bokola.github.io/posts/2023-12-16-R-package
dev/.